namespace CSharpPro._1103;

using System;
using System.Collections.Generic;
using System.Linq;

public class UserServiceImpl : IUserService
    {
        private readonly List<User> _registeredUsers = new List<User>();
        
        public string Register(string username, string password, string email = "")
        {
            try
            {
                bool isUsernameExists = false;
                foreach (var user in _registeredUsers)
                {
                    if (user.Username.Equals(username, StringComparison.OrdinalIgnoreCase))
                    {
                        isUsernameExists = true;
                        break;
                    }
                }
                if (isUsernameExists)
                    return $"注册失败：用户名「{username}」已被占用！";

                User newUser = new User(username, password, email);
                
                _registeredUsers.Add(newUser);
                return $"注册成功！用户名：{username}，邮箱（若有）：{email}";
            }
            catch (ArgumentException ex)
            {
                return $"注册失败：{ex.Message}";
            }
            catch (Exception ex)
            {
                return $"注册异常：{ex.Message}";
            }
        }
        
        public string Login(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
                return "登录失败：用户名或密码不能为空！";
            
            foreach (var user in _registeredUsers)
            {
                if (user.Username.Equals(username, StringComparison.OrdinalIgnoreCase) 
                    && user.Password.Equals(password))
                {
                    return $"登录成功！欢迎您，{user.Username}！";
                }
            }

           
            return "登录失败：用户名或密码错误！";
        }
        
        public void ShowMenu()
        {
            Console.Clear();
            Console.WriteLine("================ 用户系统 ================");
            Console.WriteLine("1. 注册新用户");
            Console.WriteLine("2. 用户登录");
            Console.WriteLine("3. 删除用户（测试功能）");
            Console.WriteLine("4. 退出系统");
            Console.WriteLine("==========================================");
            Console.Write("请选择操作（1-4）：");
        }
        public void ExecuteOperation()
        {
            while (true)
            {
                ShowMenu();
                string input = Console.ReadLine();
                switch (input)
                {
                    case "1":
                        Console.Write("请输入用户名：");
                        string regUsername = Console.ReadLine();
                        Console.Write("请输入密码（≥6位）：");
                        string regPwd = Console.ReadLine();
                        Console.Write("请输入邮箱（可选，直接回车跳过）：");
                        string regEmail = Console.ReadLine();
                        string regResult = Register(regUsername, regPwd, regEmail);
                        Console.WriteLine(regResult);
                        break;

                    case "2":
                        Console.Write("请输入用户名：");
                        string loginUsername = Console.ReadLine();
                        Console.Write("请输入密码：");
                        string loginPwd = Console.ReadLine();
                        string loginResult = Login(loginUsername, loginPwd);
                        Console.WriteLine(loginResult);
                        break;

                    case "3":
                        Console.Write("请输入待删除的用户名：");
                        string delUsername = Console.ReadLine();
                        string delResult = DeleteUser(delUsername);
                        Console.WriteLine(delResult);
                        break;

                    case "4":
                        Console.WriteLine("退出系统成功！");
                        return; 

                    default:
                        Console.WriteLine("输入错误，请选择1-4之间的数字！");
                        break;
                }
                Console.WriteLine("\n按任意键继续...");
                Console.ReadKey();
            }
        }

        public string DeleteUser(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
                return "删除失败：用户名不能为空！";
            
            for (int i = _registeredUsers.Count - 1; i >= 0; i--)
            {
                if (_registeredUsers[i].Username.Equals(username, StringComparison.OrdinalIgnoreCase))
                {
                    _registeredUsers.RemoveAt(i); 
                    return $"删除成功：用户名「{username}」已移除！";
                }
            }

            return $"删除失败：未找到用户名「{username}」！";
        }
    }
